package com.ooqiu.gaming.api.repository;

import com.ooqiu.gaming.GamingApp;
import com.ooqiu.gaming.api.GamingApi;
import com.ooqiu.gaming.api.cache.CacheProviders;
import com.ooqiu.gaming.api.entities.ActRes;
import com.ooqiu.gaming.api.entities.Article;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.rx_cache2.DynamicKey;
import io.rx_cache2.EvictProvider;
import io.rx_cache2.internal.RxCache;
import io.victoralbertos.jolyglot.GsonSpeaker;

/**
 * @author Eenie
 * @date 2017/11/22
 * 资讯的仓库
 */

public class InformationRepository {
    private GamingApi mGamingApi;
    private CacheProviders mCacheProviders;

    @Inject
    public InformationRepository(GamingApp gamingApp, GamingApi gamingApi) {
        mGamingApi = gamingApi;
        mCacheProviders = new RxCache.Builder()
                .useExpiredDataIfLoaderNotAvailable(true)
                .persistence(gamingApp.getFilesDir(), new GsonSpeaker())
                .using(CacheProviders.class);
    }

    public Observable<List<Article>> fetchNewsList(int page, int typeId) {
        return mCacheProviders.fetchNewsList(mGamingApi.fetchNewsList(page, typeId), new DynamicKey(String.format("page=%s&cat_id=%s", page, typeId)))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    public Observable<List<Article>> fetchVideoList(int page, int typeId) {
        return mCacheProviders.fetchVideoList(mGamingApi.fetchVideoList(page, typeId), new DynamicKey(String.format("page=%s&cat_id=%s", page, typeId)))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    public Observable<ActRes> fetchActivity(boolean evivt) {
        return mCacheProviders.fetchActivity(mGamingApi.fetchAct(), new EvictProvider(evivt))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }


}
